package cn.gdrj.est.service.impl;

import cn.gdrj.est.service.BrandClassifyRelationService;
import cn.gdrj.est.constants.Constants;
import cn.gdrj.est.dao.ClassifyDao;
import cn.gdrj.est.dto.BrandDTO;
import cn.gdrj.est.dto.ClassifyDTO;
import cn.gdrj.est.dto.ModelDTO;
import cn.gdrj.est.entity.Classify;
import cn.gdrj.est.entity.Model;
import cn.gdrj.est.service.BrandService;
import cn.gdrj.est.service.ClassifyService;
import cn.gdrj.est.service.ModelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 类别业务组件
 *
 * @author jun
 * @date 2021/11/8
 */
@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Autowired
    private ClassifyDao classifyDao;
    @Autowired
    private BrandClassifyRelationService brandClassifyRelationService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ModelService modelService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public List<ClassifyDTO> listClassifyTree() {
        // 查询缓存
        List<ClassifyDTO> classifyDTOS = (List<ClassifyDTO>) redisTemplate
                .opsForValue()
                .get(Constants.Redis.KEY_HOME_TREE);
        if(!ObjectUtils.isEmpty(classifyDTOS)){
            return classifyDTOS;
        }

        // 查询类别数据
        classifyDTOS =
                listClassifyBy(Constants.Status.SALE,Constants.EST_NOT);

        for (ClassifyDTO classifyDTO : classifyDTOS) {
            // 根据类别查询品牌id
            Long classifyId = classifyDTO.getId();
            List<Long> brandIds = brandClassifyRelationService
                    .listBrandIdsByClassifyId(classifyId);

            // 根据品牌id查询品牌
            List<BrandDTO> brandDTOS = brandService
                    .listByIdsAndStatus(brandIds, Constants.Status.SALE);

            classifyDTO.setBrandList(brandDTOS);

            // 根据类别和品牌查询商品
            for (BrandDTO brandDTO : brandDTOS) {
                Long brandId = brandDTO.getId();

                Model model = new Model();
                model.setClassifyId(classifyId);
                model.setBrandId(brandId);
                model.setStatus(Constants.Status.SALE);
                model.setIsDelete(Constants.EST_NOT);
                List<ModelDTO> modelDTOS = modelService
                        .listBy(model, 1, 5);

                brandDTO.setModelList(modelDTOS);
            }
        }

        // 保存到缓存中
        redisTemplate.opsForValue()
                .set(Constants.Redis.KEY_HOME_TREE,classifyDTOS,2, TimeUnit.HOURS);

        return classifyDTOS;
    }

    private List<ClassifyDTO> listClassifyBy(Integer status, Integer isDelete) {
        Classify classify = new Classify();
        classify.setStatus(status);
        classify.setIsDelete(isDelete);
        List<Classify> classifies = classifyDao.listBy(classify);

        List<ClassifyDTO> classifyDTOS = new ArrayList<>();
        for (Classify c : classifies) {
            ClassifyDTO dto = new ClassifyDTO();
            BeanUtils.copyProperties(c,dto);
            classifyDTOS.add(dto);
        }
        return classifyDTOS;
    }
}
